package com.zf.yichat.controller.bank;

import com.github.pagehelper.PageHelper;
import com.zf.yichat.controller.BaseController;
import com.zf.yichat.dto.UserBankDto;
import com.zf.yichat.dto.response.UserDto;
import com.zf.yichat.dto.response.UserIpDto;
import com.zf.yichat.mapper.UserBankMapper;
import com.zf.yichat.model.User;
import com.zf.yichat.model.UserBank;
import com.zf.yichat.utils.common.DtoChangeUtils;
import com.zf.yichat.utils.common.N3d;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.internal.util.Contracts;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 用户银行卡
 */
@Controller
@RequestMapping("bank")
@Slf4j
public class BankController extends BaseController {


    @Resource
    private UserBankMapper userBankMapper;

    /**********************访问列表页面方法开始******************************/

    /**
     * 用户列表
     * @return
     */
    @RequestMapping("/list")
    public String index() {
        return "bank/list";
    }

    /**
     * 用户列表-编辑
     * @return
     */
    @RequestMapping("/edit")
    public String edit(Long userId, String bankNumber) {
        Example example = new Example(UserBank.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", userId);
        criteria.andEqualTo("bankNumber", bankNumber);

        UserBank userBank = userBankMapper.selectOneByExample(example);
        getRequest().setAttribute("userBank", userBank);
        return "bank/edit";
    }
    /**
     * 用户列表-新增
     * @return
     */
    @RequestMapping("/add")
    public String add(Long userId) {
        return "bank/add";
    }

    /**********************访问列表页面方法结束******************************/


    @RequestMapping("/data/list")
    @ResponseBody
    public FsResponse list(Integer page, Integer limit, Long userId, String name, String mobile, String idNumber,
                           String bankName, String childBankname, String bankNumber, String recommendCode, Integer status) {

//        Example example = new Example(UserBank.class);
//        Example.Criteria criteria = example.createCriteria();
//        if (null != userId) {
//            criteria.andEqualTo("userId", userId);
//        }
//        if (StringUtils.isNotBlank(name)) {
//            criteria.andLike("name", "%" + name + "%");
//        }
//        if (StringUtils.isNotBlank(mobile)) {
//            criteria.andLike("mobile", "%" + mobile + "%");
//        }
//        if (StringUtils.isNotBlank(idNumber)) {
//            criteria.andLike("idNumber", "%" + idNumber + "%");
//        }
//        if (StringUtils.isNotBlank(bankName)) {
//            criteria.andLike("bankName", "%" + bankName + "%");
//        }
//        if (StringUtils.isNotBlank(childBankname)) {
//            criteria.andLike("childBankname", "%" + childBankname + "%");
//        }
//        if (StringUtils.isNotBlank(bankNumber)) {
//            criteria.andLike("bankNumber", "%" + bankNumber + "%");
//        }
//
//        example.setOrderByClause(" id desc");
        PageHelper.startPage(page, limit);

        List<UserBankDto> list = userBankMapper.queryPageList( userId,  name,  mobile,  idNumber,
                 bankName,  childBankname,  bankNumber,  recommendCode, status);

        return DtoChangeUtils.getPageList(list, data -> {
            UserBankDto userBank = new UserBankDto();
            BeanUtils.copyProperties(data, userBank);
            return userBank;
        });
    };

    /**
     * 编辑银行卡信息
     * @param bank
     * @return
     */
    @RequestMapping("editBank")
    @ResponseBody
    public FsResponse editBank(UserBank bank) {
        bank.setUtime(new Date());
        userBankMapper.updateByPrimaryKeySelective(bank);
        return FsResponseGen.success();
    }

    /**
     * 新增银行卡信息
     * @param bank
     * @return
     */
    @RequestMapping("addBank")
    @ResponseBody
    public FsResponse addBank(UserBank bank) {
        bank.setCtime(new Date());
        bank.setStatus(0);
        // 生产时间戳+userId的字符串
        String code = Long.toString(bank.getUserId()) + Long.toString(System.currentTimeMillis());
        bank.setHjbindcardno(code);
        bank.setHjbindcardordercode(code);
        userBankMapper.insert(bank);
        return FsResponseGen.success();
    }

    /**
     * 删除银行卡信息
     * @param bank
     * @return
     */
    @RequestMapping("delBank")
    @ResponseBody
    public FsResponse delBank(UserBank bank) {
        Example example = new Example(UserBank.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", bank.getUserId());
        criteria.andEqualTo("bankNumber", bank.getBankNumber());

        userBankMapper.deleteByExample(example);
        return FsResponseGen.success();
    }
}
